package com.nidu.demo.notify.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 站内信消息领域实体
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class NotifyMessage extends BaseTenantEntity implements BaseDeletableEntity {

    private Long id;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 用户类型
     */
    private Integer userType;

    /**
     * 模版编号
     */
    private Long templateId;

    /**
     * 模板编码
     */
    private String templateCode;

    /**
     * 模版发送人名称
     */
    private String templateNickname;

    /**
     * 模版内容
     */
    private String templateContent;

    /**
     * 模版类型
     */
    private Integer templateType;

    /**
     * 模版参数
     */
    private Map<String, Object> templateParams;

    /**
     * 是否已读
     */
    private Boolean readStatus;

    /**
     * 阅读时间
     */
    private LocalDateTime readTime;

    /**
     * 是否删除
     */
    private Boolean deleted;

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.NOTIFY_MESSAGE_NOT_FOUND.getCode(), ErrorCodeConstants.NOTIFY_MESSAGE_NOT_FOUND.getMessage());
        }
    }

    public void isRead() {
        if (BooleanEnum.TRUE.getValue().equals(this.getReadStatus())) {
            throw new BizException(ErrorCodeConstants.NOTIFY_MESSAGE_IS_READ.getCode(), ErrorCodeConstants.NOTIFY_MESSAGE_IS_READ.getMessage());
        }
    }

    public static NotifyMessage create(Long userId, Integer userType, Long templateId, String templateCode, String templateNickname, String templateContent, Integer templateType, Map<String, Object> templateParams) {
        NotifyMessage notifyMessage = new NotifyMessage();
        return notifyMessage.setUserId(userId)
                .setUserType(userType)
                .setTemplateId(templateId)
                .setTemplateCode(templateCode)
                .setTemplateNickname(templateNickname)
                .setTemplateContent(templateContent)
                .setTemplateType(templateType)
                .setTemplateParams(templateParams);
    }

}
